//
//  SpecialDetailsViewController.m
//  Share_Home_F
//
//  Created by dllo on 15/4/22.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SpecialDetailsViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "SpecialDetailsModel.h"
#import "SpecialDetaialsCell.h"
#import <ShareSDK/ShareSDK.h>
#import "DataBaseHandle.h"
@interface SpecialDetailsViewController ()
{
    UIWebView *webView;
}
@property (nonatomic, copy)NSString *lastStr;
@property (nonatomic, retain)UITableView *tableView;

@end

@implementation SpecialDetailsViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor orangeColor];
    
    self.title = self.titleStr;
   
    webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:webView];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    NSString *str1 = @"http://ihome.cmfmobile.com:8080/sp/custom/getNewSpecialSingle.do?id=";
    NSString *str = [NSString stringWithFormat:@"%@%@", str1, self.str];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *arr = responseObject;
        for (NSInteger i = 0; i < [arr count]; i++) {
            NSMutableDictionary *dic = arr[i];
            SpecialDetailsModel *specialDM = [[SpecialDetailsModel alloc] init];
            [specialDM setValuesForKeysWithDictionary:dic];
            self.lastStr = specialDM.shareContent;
        }
   
    
    [webView loadHTMLString:self.lastStr baseURL:nil];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error");
    }];
#pragma mark 分享按钮
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fenxiang"] style:UIBarButtonItemStylePlain target:self action:@selector(selectRightAction:)];
  
    self.navigationItem.rightBarButtonItem = rightButton;
#pragma mark 收藏按钮
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-shoucang"] style:UIBarButtonItemStylePlain target:self action:@selector(selectLeftAction:)];
    
    self.navigationItem.leftBarButtonItem = leftButton;
#pragma mark 添加手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
        [webView addGestureRecognizer:pan];
        [pan release];
}

-(void)selectLeftAction:(id)sender {
    // 收藏按钮
    // 打开数据库
    [[DataBaseHandle shareDataBaseHandle] openDB];
    // 创建表
    [[DataBaseHandle shareDataBaseHandle] createTable];
    //插入对象
    Sqlite *sql = [[Sqlite alloc] init];
    sql.id = self.str;
    sql.title = self.titleStr;
    sql.keyword = self.key;
    sql.introduction = self.intro;
    sql.pic = self.picStr;
    [[DataBaseHandle shareDataBaseHandle] insertSql:sql];
    

    [[DataBaseHandle shareDataBaseHandle] closeDB];
    
    
    
}
-(void)selectRightAction:(id)sender {
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"0" ofType:@"png"];
    
    //1、构造分享内容
    id<ISSContent> publishContent = [ShareSDK content:@"要分享的内容"
                                       defaultContent:@"默认内容"
                                                image:[ShareSDK imageWithPath:imagePath]
                                                title:@"ShareSDK"
                                                  url:@"http://www.mob.com"
                                          description:@"这是一条演示信息"
                                            mediaType:SSPublishContentMediaTypeNews];
    //1+创建弹出菜单容器（iPad必要）
    id<ISSContainer> container = [ShareSDK container];
//    [container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDirectionUp];
    
    //2、弹出分享菜单
    [ShareSDK showShareActionSheet:container
                         shareList:nil
                           content:publishContent
                     statusBarTips:YES
                       authOptions:nil
                      shareOptions:nil
                            result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                //可以根据回调提示用户。
    if (state == SSResponseStateSuccess)
    {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"恭喜您"message:@"分享成功么么哒" delegate:self  cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        }
else if (state == SSResponseStateFail)
    {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"很遗憾"message:@"分享失败了呢" delegate:self
cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
      }
    }];
}

-(void)panAction:(id)sender {
    CATransition *transition = [CATransition animation];
    transition.duration = 1;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    transition.type = @"oglFlip";
    transition.subtype = kCATransitionFromLeft;
    transition.delegate = self;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    [self.navigationController popViewControllerAnimated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
